package com.mazaiting.instructions.math;

import com.mazaiting.instructions.base.NoOperandsInstruction;
import com.mazaiting.rtda.MtFrame;
import com.mazaiting.rtda.MtOperandStack;

/**
 * 减法指令
 *
 * @author mazaiting
 * @since 2021/8/14
 */
public class SubInstruction {

    /**
     * 双精度浮点 减法
     */
    public static class DSub extends NoOperandsInstruction {
        @Override
        public void execute(MtFrame frame) {
            MtOperandStack stack = frame.getOperandStack();
            double v1 = stack.popDouble();
            double v2 = stack.popDouble();
            double result = v2 - v1;
            stack.pushDouble(result);
        }
    }

    /**
     * 单精度浮点 减法
     */
    public static class FSub extends NoOperandsInstruction {
        @Override
        public void execute(MtFrame frame) {
            MtOperandStack stack = frame.getOperandStack();
            float v1 = stack.popFloat();
            float v2 = stack.popFloat();
            float result = v2 - v1;
            stack.pushFloat(result);
        }
    }

    /**
     * 整型 减法
     */
    public static class ISub extends NoOperandsInstruction {
        @Override
        public void execute(MtFrame frame) {
            MtOperandStack stack = frame.getOperandStack();
            int v1 = stack.popInt();
            int v2 = stack.popInt();
            int result = v2 - v1;
            stack.pushInt(result);
        }

        @Override
        public String toString() {
            return "isub";
        }
    }

    /**
     * 长整型 减法
     */
    public static class LSub extends NoOperandsInstruction {
        @Override
        public void execute(MtFrame frame) {
            MtOperandStack stack = frame.getOperandStack();
            long v1 = stack.popLong();
            long v2 = stack.popLong();
            long result = v2 - v1;
            stack.pushLong(result);
        }

        @Override
        public String toString() {
            return "lsub";
        }
    }

}
